<?php


/**
 * @author Kevin
 * @version 1.0
 * @created 12-d�c.-2011 19:51:41
 */
class Sport
{

	var $bookid = NULL;
	/**
	 * Déclarations des données
	 */
	var $id = NULL;
	var $name = NULL;


	/**
	 * Constructeur
	 * 
	 * @param pId
	 */
	function Sport($pId)
	{
		global $wpdb;
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM sports WHERE id = %d", $pId));
		
		$this->id = $pId;
		$this->name = $_row->name;
		$this->bookid = $_row->bookid;
	}

	public function setBookID($bookid)
	{
	    $this->bookid = $bookid;
	}
	
	public function getBookID()
	{
	    return $this->bookid;
	}

	/**
	 * ====================== Récupération du nombre de pari disponible pour ce
	 * sport ======================
	 */
	function getCountBetsAvailable()
	{
		global $wpdb;
		return 
		$wpdb->get_var(
			$wpdb->prepare(
				// request
				"SELECT count(*)
				FROM bets b 
				JOIN events e ON b.fk_event = e.id
				JOIN leagues l ON e.fk_league = l.id
				JOIN publications p ON e.id = p.fk_concretid							
				WHERE l.fk_sport = %d	
				AND	(	( p.date > '%s')
						 OR
						( p.date = '%s' AND p.hour > '%s')
				)",
				
				// params
				$this->id,
				date("Y-m-d"),
				date("Y-m-d"),
				date("H:i:s")
			)
		);
	}

	/**
	 * ====================== Récupération de la liste des events rattachés à ce
	 * sport ======================
	 */
	function getEvents()
	{
		global $wpdb;
		return
		$wpdb->get_results(
			$wpdb->prepare(
				// request
				"SELECT e.id, e.fk_league, p.date, p.hour
				FROM EVENTS e
				JOIN leagues l ON e.fk_league = l.id
				JOIN publications p ON e.id = p.fk_concretid
				WHERE l.fk_sport =  '%d'
				AND p.type =  'EVENT'
				AND p.fk_concretid = e.id
				AND (p.date > '%s' OR (p.date =  '%s' AND p.hour > %s))
				ORDER BY p.date, e.fk_league, p.hour",
				
				// params
				$this->id,
				date('Y-m-d'),
				date('Y-m-d'),
				date('H:i:s')
			)
		);
	}
	
	function getEventsByDate($date)
	{
		global $wpdb;
		
		$request = "";
		if($date == date('Y-m-d')){
			$request = "SELECT e.id, e.fk_league, p.date, p.hour
						FROM EVENTS e
						JOIN leagues l ON e.fk_league = l.id
						JOIN publications p ON e.id = p.fk_concretid
						WHERE l.fk_sport =  '%d'
						AND p.type =  'EVENT'
						AND p.fk_concretid = e.id
						AND (p.date =  '%s' AND p.hour > %s)
						ORDER BY p.date, e.fk_league, p.hour";
		}else{
			$request = "SELECT e.id, e.fk_league, p.date, p.hour
						FROM EVENTS e
						JOIN leagues l ON e.fk_league = l.id
						JOIN publications p ON e.id = p.fk_concretid
						WHERE l.fk_sport =  '%d'
						AND p.type =  'EVENT'
						AND p.fk_concretid = e.id
						AND (p.date =  '%s')
						ORDER BY p.date, e.fk_league, p.hour";
		}

		
		return
		$wpdb->get_results(
			$wpdb->prepare(
				// request
				$request,
			
				// params
				$this->id,
				$date,
				date('H:i:s')
			)
		);
	}

	/**
	 * ====================== Get/Set ======================
	 */
	function getID()
	{
		return $this->id;
	}

	/**
	 * ====================== Récupération de la liste des leagues rattachés à ce
	 * sport ======================
	 */
	function getLeagues()
	{
		global $wpdb;
		return 
		$wpdb->get_results(
			$wpdb->prepare(
				// request
				"SELECT id 
				FROM leagues 
				WHERE fk_sport = %d",
		
				// params
				$this->id
			)
		);
	}

	function getName()
	{
		return getTranslation('sports', $this->name);
	}

}
?>